package com.cskaoyan.service.vx.search;

import com.cskaoyan.bean.bo.vxBo.search.SearchClearBO;
import com.cskaoyan.bean.po.wxPo.*;
import com.cskaoyan.bean.vo.vxVo.search.HistoryKeywordVo;
import com.cskaoyan.bean.vo.vxVo.search.HotKeywordVo;
import com.cskaoyan.bean.vo.vxVo.search.SearchIndexVo;
import com.cskaoyan.mapper.wxMapper.MarketKeywordMapper;
import com.cskaoyan.mapper.wxMapper.po2votransfer.MarketKeywordTransferMapper;
import com.cskaoyan.mapper.wxMapper.MarketSearchHistoryMapper;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: 陈慧颖
 * @time: Created by chy on 2022/6/7 15:38
 */

@Service
public class SearchServiceImpl  implements SearchService{
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Autowired
    MarketKeywordMapper marketKeywordMapper;
    @Override
    public SearchIndexVo index() {

        /**
         * 修订bug:   发现:HistoryKeyword是 sql: market_keyword 的 keyword 字段;
         */
        MarketKeywordExample briefKeyword = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = briefKeyword.createCriteria();
        List<MarketKeyword> briefKeywordList = marketKeywordMapper.selectByExample(briefKeyword);

        //
        // for (MarketKeyword marketKeyword : briefKeywordList) {
        //     HistoryKeywordVo historyKeywordVo = new HistoryKeywordVo();
        //     historyKeywordVo.setKeyword(marketKeyword.getKeyword());
        //     historyKeywordVos.add(historyKeywordVo);
        // }
        /**
         * 从 market_search_history 表获取: 所搜的那个keyword;
         */
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        marketSearchHistoryExample.createCriteria().andDeletedEqualTo(false);
        List<MarketSearchHistory> marketSearchHistories =
                marketSearchHistoryMapper.selectByExample(marketSearchHistoryExample);

        ArrayList<HistoryKeywordVo> historyKeywordVos = new ArrayList<>();

        for (MarketSearchHistory marketSearchHistory : marketSearchHistories) {
            HistoryKeywordVo historyKeywordVo = new HistoryKeywordVo();
            historyKeywordVo.setKeyword(marketSearchHistory.getKeyword());
            historyKeywordVos.add(historyKeywordVo);
        }


        // sql 读取 market_keyword;
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria1 = marketKeywordExample.createCriteria();
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
       ;

        // 2. 将 List<MarketSearchHistory> marketSearchHistories转化为  List<HistoryKeywordVo> historyKeyword;
        // 3. 将 List<MarketKeyword> marketKeywords转化为 List<HotKeywordVo> hotKeyword;
        // keywordPo->keywordVo 转化;
        List<HotKeywordVo> hotKeywordVos =
                MarketKeywordTransferMapper.INSTANCE.poList2VoList(marketKeywords);


        for (HotKeywordVo hotKeywordVo : hotKeywordVos) {
        hotKeywordVo.setIsHot(true);
        hotKeywordVo.setIsDefault(true);
        }


        SearchIndexVo searchIndexVo = new SearchIndexVo();
        /**
         * todo: 先 new 一个 list,占座
         */
        /**
         * 需求是  未登录不会显示搜索历史记录;   已登录才显示搜索历史记录;  每次输入会新增一条历史记录;
         */
        searchIndexVo.setHistoryKeywordList(historyKeywordVos); //marketSearchHistories Po->VXCollectAddOrDeleteBo, 先使用一个占位符
        // setters
        searchIndexVo.setHotKeywordList(hotKeywordVos);
        searchIndexVo.setDefaultKeyword(hotKeywordVos.get(0));
        return  searchIndexVo;
    }

    /**
     * 响应 /wx/search/helper;
     * 传入参数 keyword;
     * 应将该历史记录写入数据库;
     * @param keyword
     * @return
     */
    @Override
    public List<String> helper(String keyword) {
        MarketUser marketUser = (MarketUser)SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();

        //
        // strings.add(keyword);
        // return strings  ;

        // sql :PO
        MarketSearchHistory marketSearchHistory = new MarketSearchHistory();
        marketSearchHistory.setId(null);
        marketSearchHistory.setKeyword(keyword);
        marketSearchHistory.setUserId(marketUser.getId());
        marketSearchHistory.setAddTime(new Date());
        marketSearchHistory.setUpdateTime(new Date());
        marketSearchHistory.setDeleted(false);
        marketSearchHistory.setFrom("第七区");

        int insert = marketSearchHistoryMapper.insert(marketSearchHistory);
        ArrayList<String> strings = new ArrayList<>();
        MarketKeywordExample marketKeywordExample = new MarketKeywordExample();
        marketKeywordExample.createCriteria().andDeletedEqualTo(false);
        List<MarketKeyword> marketKeywords = marketKeywordMapper.selectByExample(marketKeywordExample);
        for (MarketKeyword marketKeyword : marketKeywords) {
            strings.add(marketKeyword.getKeyword());
        }
        return  strings;

    }

    /**
     * 与 sql: market_search_history交互;
     * 清除历史记录;
     */
    @Override
    public void clearhistory(SearchClearBO searchClearBO) {
        // new 一个 Example 实例;
        MarketSearchHistoryExample marketSearchHistoryExample = new MarketSearchHistoryExample();
        // 给该Ex ample 实例添加一些需要的 关系条件;
        MarketSearchHistoryExample.Criteria criteria = marketSearchHistoryExample.createCriteria();
        criteria.andDeletedEqualTo(false); // 表示  deleted = 0 ;
        // 调用该deleteByExample方法, 将 exapmle 实例传入;
        int count =  marketSearchHistoryMapper.deleteByExample(marketSearchHistoryExample);
        return ;

    }
}
